/*
 * Copyright@www.it1997.com.
 * @Author: 陈晓晨
 * @Date: 2020/6/8
 * Description:版权所有 QQ:376231445
 */

package com.it1997.it1997admin.utils;

import com.it1997.it1997admin.pojo.User;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JwtTokenUtil {
    //token请求头
    private static final String TOKEN_HEADER="Authorization";
    //token前缀
    private static final String PREFIX="Bearer";
    //签名主题
    private static final String SUBJECT="cxc";
    //过期时间
    private static final String EXPIRITION="1000 * 24 * 60 * 60 * 7";
    // 应用密钥
    public static final String APPSECRET_KEY = "MyJwtSecret";
    // 角色权限声明
    private static final String ROLE_CLAIMS = "role";
    /*
    * 生成token
    * */
    public static String createToken(String username,String role){
            Map<String,Object> map = new HashMap<>();
            map.put(ROLE_CLAIMS, role);
            String token = Jwts
                    .builder()
                    .setSubject(username)
                    .setClaims(map)
                    .claim("username",username)
                    .setIssuedAt(new Date())
                    .setExpiration(new Date(System.currentTimeMillis() + EXPIRITION))
                    .signWith(SignatureAlgorithm.HS256, APPSECRET_KEY).compact();
            return token;
    }
    /*
    * 检验token
    * */
    public static Claims checkJWT(String token) {
        try {
            final Claims claims = Jwts.parser().setSigningKey(APPSECRET_KEY).parseClaimsJws(token.replace("Bearer ", "")).getBody();
            return claims;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 从Token中获取username
     */
    public static String getUsername(String token){
        Claims claims = Jwts.parser().setSigningKey(APPSECRET_KEY).parseClaimsJws(token.replace("Bearer ", "")).getBody();
        return claims.get("username").toString();
    }
    /**
     * 从Token中获取用户角色
     */
    public static String getUserRole(String token){
        Claims claims = Jwts.parser().setSigningKey(APPSECRET_KEY).parseClaimsJws(token.replace("Bearer ", "")).getBody();
        return claims.get("role").toString();
    }
    /**
     * 从Token中获取用户
     */
    public static List<GrantedAuthority> getAuthorities(String token){
        Claims claims = Jwts.parser().setSigningKey(APPSECRET_KEY).parseClaimsJws(token.replace("Bearer ", "")).getBody();
        return  AuthorityUtils.commaSeparatedStringToAuthorityList((String) claims.get("authorities"));
    }
    /**
     * 校验Token是否过期
     */
    public static boolean isExpiration(String token){
        Claims claims = Jwts.parser().setSigningKey(APPSECRET_KEY).parseClaimsJws(token.replace("Bearer ", "")).getBody();
        return claims.getExpiration().before(new Date());
    }
}
